package com.seirsoft.framework.payload;

public class PayloadTable
{
    // Fields
    private PayloadColumnCollection m_columns;
    private PayloadRowCollection m_rows;
    
    private String m_name;
    
    
    public PayloadTable()
    {
    	this(null);
    }
    
    public PayloadTable(String strName)
    {
    	if(strName != null)
    	{
    		this.setName(strName);
    	}
    	
    	m_columns = new PayloadColumnCollection();
    	m_rows = new PayloadRowCollection();
    }
    
    public PayloadColumnCollection getColumns()
    {
    	return this.m_columns;
    }
    
    public PayloadRowCollection getRows()
    {
    	return this.m_rows;
    }
  
    public PayloadRow getNewRow()
    {
    	return new PayloadRow(this);
    }
    
	private void setName(String strName)
	{
		this.m_name = strName;
	}
	
	public String getName()
	{
		return this.m_name;
	}
	

    /**
     *  Factory class that keeps the parse method
     */
    public static class Factory {
        /**
         * static method to create the object
         * Precondition:  If this object is an element, the current or next start element starts this object and any intervening reader events are ignorable
         *                If this object is not an element, it is a complex type and the reader is at the event just after the outer start element
         * Postcondition: If this object is an element, the reader is positioned at its end element
         *                If this object is a complex type, the reader is positioned at the end element of its outer element
         */
        public static PayloadTable parse(
            javax.xml.stream.XMLStreamReader reader) throws java.lang.Exception {
        	PayloadTable object = new PayloadTable();

            try {            
            	// <table>
                while (!reader.isStartElement() && !reader.isEndElement())
                    reader.next();

                String strTableName = reader.getAttributeValue("", "name");
                object.setName(strTableName);

                
                do{
                	reader.next();
                } while (!reader.isStartElement() && !reader.isEndElement());
                
                if (reader.isStartElement()
                		&& reader.getLocalName().equals("columns"))
                {               	
                	PayloadColumnCollection.Factory.parse(reader, object);
                }
                
                do{
                	reader.next();
                } while (!reader.isStartElement() && !reader.isEndElement());

                if (reader.isStartElement()
                	&& reader.getLocalName().equals("rows"))
                {
                	PayloadRowCollection.Factory.parse(reader, object);
                }
                
                // must be </table>
            } catch (javax.xml.stream.XMLStreamException e) {
                throw new java.lang.Exception(e);
            }

            return object;
        }
    } //end of factory class

}
